/*
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.action;

import java.lang.reflect.InvocationTargetException;

import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.resources.RemoteFile;
import net.sourceforge.perforce.ui.PerforceUIPlugin;
import net.sourceforge.perforce.ui.internal.Policy;
import net.sourceforge.perforce.ui.internal.RemoteFileEditorInput;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class OpenRemoteFileAction extends PerforceAction {
  //----------------------------------------------------------------------------
  // -- Public Methods

  @Override
  public void run(IAction action) {
    run(new IRunnableWithProgress() {
      @SuppressWarnings("unused")
      // InterruptedException
      public void run(IProgressMonitor monitor) throws InvocationTargetException,
          InterruptedException {
        IWorkbench workbench = PerforceUIPlugin.getPlugin().getWorkbench();
        IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();

        IEditorRegistry registry = workbench.getEditorRegistry();

        RemoteFile[] files = getSelectedRemoteFiles();
        for (int i = 0; i < files.length; i++) {
          if (files[i].isDeleted()) {
            MessageDialog.openError(getShell(), Policy.bind("OpenLogEntryAction.deletedTitle"), //$NON-NLS-1$
                Policy.bind("OpenLogEntryAction.deleted")); //$NON-NLS-1$
          } else {
            String filename = files[i].getName();
            IEditorDescriptor descriptor = registry.getDefaultEditor(filename);

            String id;
            if (descriptor == null) {
              id = "org.eclipse.ui.DefaultTextEditor"; //$NON-NLS-1$
            } else {
              id = descriptor.getId();
            }

            try {
              page.openEditor(new RemoteFileEditorInput(files[i]), id);
            } catch (PartInitException e) {
              throw new InvocationTargetException(e);
            }
          }
        }
      }
    }, PROGRESS_BUSYCURSOR);
  }

  // ---------------------------------------------------------------------------
  // Protected Methods

  @SuppressWarnings("unused")
  // PerforceException
  @Override
  protected boolean isEnabled() throws PerforceException {
    RemoteFile[] files = getSelectedRemoteFiles();
    if (files.length == 0) {
      return false;
    }

    return true;
  }
}
